/**
 * Created by gf on 17.4.27.
 */
//路由配置
app.config(['$routeProvider', function ($routeProvider) {

  //其他页面
  $routeProvider.when('/', {
    templateUrl: 'pages/main.html',
    controller: 'mainCtrl'
  }).when('/main', {
    templateUrl: 'pages/main.html',
    controller: 'mainCtrl'
  }).when("/database", {
    templateUrl: 'pages/database.html',
    controller: 'databaseCtrl'
  }).when("/database-multi", {
    templateUrl: 'pages/database-multi.html',
    controller: 'databaseMultiCtrl'
  }).when("/version", {
    templateUrl: 'pages/version.html',
    controller: 'versionCtrl'
  }).when("/help", {
    templateUrl: 'pages/help.html',
    controller: 'helpCtrl'
  }).when("/done", {
    templateUrl: 'pages/done.html',
    controller: 'doneCtrl'
  }).when("/md5", {
    templateUrl: 'pages/md5.html',
    controller: 'md5Ctrl'
  });

  //参数配置//数据库部分//接口部分//平台部署
  for (var i in menus) {
    for (var j in menus[i].children) {
      var path = menus[i].children[j].path;
      $routeProvider.when("/" + path, {
        templateUrl: 'pages/' + path + '.html',
        controller: path + 'Ctrl'
      });
    }
  }

  //错误处理
  $routeProvider.when("/error", {
    templateUrl: 'pages/error.html',
    controller: 'errorCtrl'
  }).otherwise({
    redirectTo: '/error'
  });

}]).run(['$rootScope', '$location', function ($rootScope, $location) {
  /* 监听路由的状态变化 */
  $rootScope.$on('$routeChangeStart', function (event, next, current) {
    if (current && current.scope.hasChange) {
      // if (current && current.loadedTemplateUrl == "pages/platform.html" && next.$$route.templateUrl != "pages/done.html") {
      // if (current && current.loadedTemplateUrl == "pages/platform.html") {
      //   event.preventDefault();
      //   current.scope.confirmGo(next.$$route.originalPath);
      // if (!confirm("配置已修改，离开页面将不会保存修改，确认跳转？")) {
      //   event.preventDefault();
      // }
      // }

      // 其他配置页面
      var paths = isOffline ? menusOffline : menus;
      for (var i in paths[0].children) {
        var path = paths[0].children[i].path;
        if (current && current.loadedTemplateUrl == "pages/" + path + ".html") {
          if (confirm("配置已修改，跳转页面前是否保存？")) {
            if (!current.scope.checkForm())
              event.preventDefault();
            // 保存
            current.scope.saveFnc();
          }
        }
      }

    }
  });
  $rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
    // console.log(event, current, previous);
  });
}]);